package com.cangxuan.app.ui.fragment;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cangxuan.app.R;
import com.cangxuan.app.entity.Goods;
import com.cangxuan.app.ui.collection.CollectionDetailActivity;
import com.cangxuan.app.ui.collection.CommercialEquityActivity;
import com.cangxuan.app.ui.collection.PreventingSpeculationActivity;

import java.time.format.DateTimeFormatter;

/**
 * Author: 陈青石
 * Activity: 藏品详情页面（藏品信息）片段
 */
public class CollectionInfoFragment extends Fragment {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    Goods goodsDetail;
    TextView issuer;
    TextView code;
    TextView hash;
    TextView releaseDate;
    LinearLayout layoutBlockchain;

    LinearLayout layoutEquity;
    LinearLayout layoutSpeculation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View parentView = inflater.inflate(R.layout.fragment_collection_info, container, false);

        Bundle arguments = getArguments();
        goodsDetail = (Goods) arguments.getSerializable("goodsDetail");
        System.out.println("goodsDetail" + goodsDetail);
        layoutBlockchain = parentView.findViewById(R.id.layout_blockchain);
        issuer = parentView.findViewById(R.id.issuer);

        code = parentView.findViewById(R.id.code);
        hash = parentView.findViewById(R.id.hash);
        releaseDate = parentView.findViewById(R.id.release_date);
        layoutEquity = parentView.findViewById(R.id.layout_equity);
        layoutSpeculation = parentView.findViewById(R.id.layout_speculation);


        layoutBlockchain.setOnClickListener(view -> {
            showDialog("区块链信息", goodsDetail.getBlockchain());
        });
        layoutEquity.setOnClickListener(view -> {
            Intent intent=new Intent(getContext(), CommercialEquityActivity.class);
            startActivity(intent);
        });
        layoutSpeculation.setOnClickListener(view -> {
            Intent intent=new Intent(getContext(), PreventingSpeculationActivity.class);
            startActivity(intent);
        });


        issuer.setText(goodsDetail.getIssuer());
        code.setText(goodsDetail.getCode());
        hash.setText(goodsDetail.getHash());
        releaseDate.setText(goodsDetail.getReleaseDate().format(formatter));
        return parentView;
    }

    private void showDialog(String title, String value) {
        Dialog alertDialog = new AlertDialog.Builder(getContext()).
                setTitle(title).
                setMessage(value).
                create();
        alertDialog.show();
    }


}
